var app = getApp();
var memberUtil=require('../../utils/memberUtil');
Page({
  data: {
    statusType: ["待付款", "待发货", "待收货"],//, "待评价", "退款/售后"
    currentType: 0,
    tabClass: ["", "", "", "", ""],
    orderList:[],
    serverImageUrl:app.globalData.baseUrl,
  },
  //切换状态
  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });
    this.onShow();
  },
  orderDetail: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/order-details/index?id=" + orderId
    })
  },
  //取消订单
  cancelOrderTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    var orderNumber= e.currentTarget.dataset.ordernumber;
    var cancelOrderParamJson={
      memberId:that.data.userInfo.id,
      orderNumber:orderNumber,
      orderId:orderId
    }
    wx.showModal({
      title: '确定要取消该订单吗？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url:app.globalData.baseUrl + '/order/cancelOrder',
            method:'POST',
            data: JSON.stringify(cancelOrderParamJson),
            success: function(res){
              wx.hideLoading();
              if (res.data.status ==200) {
                wx.showToast({
                  title: '订单取消成功',
                  icon: 'success',
                  duration:2000
                });
                setTimeout(that.findOrderList,2000);
              }else{
                wx.showToast({
                  title: '订单取消失败',
                  icon: 'none',
                  duration: 2000
                });
                return false;
              }
            }
          })
        }
      }
    })
  },
  //去支付
  toPayTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    var belongTo = e.currentTarget.dataset.belongto;
    var totalPrice = e.currentTarget.dataset.totalprice;
    var orderNumber = e.currentTarget.dataset.ordernumber;
    var goodsname = e.currentTarget.dataset.goodsname;
    wx.redirectTo({
      url: '/pages/shopcar/buy?belongTo='+belongTo+"&totalPrice="+totalPrice+"&orderId="+orderId+"&orderNumber="+orderNumber+"&goodsname="+goodsname
    })
  },
  onLoad: function (options) {
    var that=this;
    that.setData({
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,2);
    }
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成

  },
  getOrderStatistics: function () {
    var that = this;
    wx.request({
      url: app.globalData.subDomain + '/order/statistics',
      data: { token: app.globalData.token },
      success: function(res){
        wx.hideLoading();
        if (res.data.code == 0) {
          var tabClass = that.data.tabClass;
          if (res.data.data.count_id_no_pay > 0) {
            tabClass[0] = "red-dot"
          } else {
            tabClass[0] = ""
          }
          if (res.data.data.count_id_no_transfer > 0) {
            tabClass[1] = "red-dot"
          } else {
            tabClass[1] = ""
          }
          if (res.data.data.count_id_no_confirm > 0) {
            tabClass[2] = "red-dot"
          } else {
            tabClass[2] = ""
          }
          if (res.data.data.count_id_no_reputation > 0) {
            tabClass[3] = "red-dot"
          } else {
            tabClass[3] = ""
          }
          if (res.data.data.count_id_success > 0) {
            //tabClass[4] = "red-dot"
          } else {
            //tabClass[4] = ""
          }

          that.setData({
            tabClass: tabClass,
          });
        }
      }
    })
  },
  onShow: function () {
      this.findOrderList();
  },
    findOrderList:function(){
        // 获取订单列表
        wx.showLoading({
            title: '数据加载中',
            mask: true
        });

        var that = this;
        var json = {
            memberId: that.data.userInfo.id,
            status: that.data.currentType
        };
        var url="/order/findOrderList";
        if(that.data.currentType==2){
            url="/order/waitReceivedOrder";
        }
        wx.request({
            url: app.globalData.baseUrl + url,
            data: JSON.stringify(json),
            method:'POST',
            success: function(res){

                if (Number(res.data.length)>0) {
                    that.setData({
                        orderList: res.data
                    });
                }else{
                    that.setData({
                        orderList: []
                    });
                }
                wx.hideLoading();
            }
        })
    },
    //页面跳转
    goodsDetailNavigate:function(e){
        wx.showLoading({
            title: '数据加载中',
            mask: true
        })
        wx.hideLoading();
        var goodsId=e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/shop/shopDt?goodsId='+goodsId
        })

    },

  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh: function () {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom: function () {

},

/**
 * 用户点击右上角分享
 */
onShareAppMessage: function () {
  var that=this;
  return {
    title: '我的二维码',
    path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
    imageUrl: app.globalData.loginUserInfo.erweimaUrl
  }
}
})
